package edu.nf.project.mappers.storeOperation.convention;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.nf.project.entitys.dto.storeOperation.DishDTO;
import edu.nf.project.entitys.entity.storeOperation.convention.dish.Dish;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author gjh
 * @Date 2025/6/11
 * @Description: 菜品信息数据访问层
 */
@Mapper
public interface DishDTOMapper extends BaseMapper<DishDTO> {
    /**
     * 获取所有菜品列表，包含销量信息
     * @return 菜品列表
     */
    @Select("SELECT \n" +
            "    d.dish_id,\n" +
            "    d.dish_name,\n" +
            "    d.dish_image,\n" +
            "    d.price,\n" +
            "    d.dish_status,\n" +
            "    d.dt_id,\n" +
            "    d.dish_describe,\n" +
            "    d.priority,\n" +
            "    (SELECT COALESCE(SUM(oi.quantity), 0) \n" +
            "     FROM customer_order_item oi \n" +
            "     WHERE oi.dish_id = d.dish_id) AS sold_quantity\n" +
            "FROM \n" +
            "    dish d\n" +
            "ORDER BY \n" +
            "    d.dt_id, d.priority ASC, d.dish_id;\n" +
            "    ")
    List<DishDTO> getDishList();

    /**
     * 获取销售量最高的5条菜品数据
     * @return
     */
    @Select(" SELECT d.dish_id\n" +
            "    FROM dish d\n" +
            "    LEFT JOIN customer_order_item coi ON d.dish_id = coi.dish_id\n" +
            "    GROUP BY d.dish_id\n" +
            "    ORDER BY SUM(coi.quantity) DESC\n" +
            "    LIMIT 5")
    List<Integer> findTop5DishIdsBySales();

    /**
     * 将这些 dish_id 优先级设为 1
     * @param ids
     * @param priority
     */
    @UpdateProvider(type = DishSqlProvider.class, method = "updatePriorityBatchSql")
    void updatePriorityBatch(@Param("ids") List<Integer> ids, @Param("priority") int priority);

    /**
     * 把优先级 = 1 但不在前5的改成 3
     * @param excludeIds
     */
    @UpdateProvider(type = DishSqlProvider.class, method = "resetOldTopPrioritySql")
    void resetOldTopPriority(@Param("excludeIds") List<Integer> excludeIds);

    /**
     * 查询当前状态为在售（未下架）的所有菜品
     * @return
     */
    @Select("SELECT * FROM dish WHERE dish_status != 2") // status = 2 表示下架
    List<DishDTO> findAllActiveDishes();

    /**
     * 库存不足 → 状态设置为 SOLD_OUT (1) 或 TAKEN_OFF_THE_SHELVES (2)
     * @param dishId
     */
    @Update("UPDATE dish SET dish_status = 1 WHERE dish_id = #{dishId}")
    void markDishAsOff(@Param("dishId") Integer dishId);

    /**
     * 将指定的菜品状态设为“出售中”（dish_status = 0）
     * @param dishId
     */
    @Update("UPDATE dish SET dish_status = 0 WHERE dish_id = #{dishId}")
    void markDishAsOn(@Param("dishId") Integer dishId);



    @Select({
            "<script>",
            "SELECT * FROM dish",
            "WHERE dish_id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<DishDTO> selectBatchByIds(@Param("ids") List<Integer> ids);

}
